﻿using MediatR;
using ProductionManagement.Application.Command.ScrapsCommand;
using ProductionManagement.Infrstructre.lnterface;

namespace ProductionManagement.Application.CommandHandler.ScrapsCommandHandler
{
    public class DelScrapsCommandHandler : IRequestHandler<DelScrapsCommand, int>
    {
        IProductionWasteRepository pwRep;

        IProduceScrapsProductsRepository pspRep;

        public DelScrapsCommandHandler(IProduceScrapsProductsRepository pspRep, IProductionWasteRepository pwRep)
        {
            this.pspRep = pspRep;
            this.pwRep = pwRep;
        }


        public async Task<int> Handle(DelScrapsCommand request, CancellationToken cancellationToken)
        {
            var list = (await pwRep.GetAll()).Where(c => request.PSPId.Contains(c.PWId)).ToList();
            foreach (var item in list)
            {
                item.IsDel = true;
                item.DelTime = DateTime.Now;
                item.DelName=request.DelName;
                await pwRep.UpdAsync(item);
            }
            return 1;
        }
    }
}
